//
//  RMTimeHandle.m
//  RMNote
//
//  Created by lxf on 16/4/15.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMTimeHandle.h"
#import "NSDate+Calendar.h"

@interface RMTimeHandle ()
@property (nonatomic, strong) NSDateComponents *comps;
@property (nonatomic, strong) NSMutableDictionary *moth;
@property (nonatomic, strong) NSMutableDictionary *yearDic;


@end

@implementation RMTimeHandle


+ (instancetype)shareInstance{
    static RMTimeHandle *timeHandel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        timeHandel = [[RMTimeHandle alloc]init];
    });
    return timeHandel;
    
}

- (NSDictionary *)dataDrawUntilMonthWithDateArr:(NSArray *)dateArr{
//    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    for (NSDate *date in dateArr) {
        [self drawUntilMonthWithDate:date];
  
    }
    return self.yearDic;
}

- (NSDictionary *)dataDrawUntilDayWithDateArr:(NSArray *)dateArr{
    //    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    for (NSDate *date in dateArr) {
        [self drawUntilDayWithDate:date];
        
    }
    return self.yearDic;
}

- (void)drawUntilMonthWithDate:(NSDate *)date{

    NSString *year = date.year;
    NSString *month = date.month;
    NSMutableDictionary *mothDic = [self.yearDic objectForKey:year];
    if (!mothDic) {
        mothDic = [NSMutableDictionary dictionary];
    }
    NSMutableArray * dayArr = [mothDic objectForKey:month];
    if (!dayArr) {
        dayArr = [NSMutableArray array];
    }
    [dayArr addObject:date];
    [mothDic setObject:dayArr forKey:month];
    [self.yearDic setObject:mothDic forKey:year];
//    return dic;
}

- (void)drawUntilDayWithDate:(NSDate *)date{
    NSString *year = date.year;
    NSString *month = date.month;
    NSString *day = date.day;
    NSMutableDictionary *mothDic = [self.yearDic objectForKey:year];
    if (!mothDic) {
        mothDic = [NSMutableDictionary dictionary];
    }
    NSMutableDictionary * dayDic = [mothDic objectForKey:month];
    if (!dayDic) {
        dayDic = [NSMutableDictionary dictionary];
    }
    NSMutableArray *timeArr = [dayDic objectForKey:day];
    if (!timeArr) {
        timeArr = [NSMutableArray array];
    }
    [timeArr addObject:date];
    [dayDic setObject:timeArr forKey:day];
    [mothDic setObject:dayDic forKey:month];
    [self.yearDic setObject:mothDic forKey:year];
    
}


- (NSDate *)dateForDateStr:(NSString *)dateStr andFormatter:(NSString *)formatter{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
//    inputFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
    [inputFormatter setDateFormat:formatter];
    NSDate* inputDate = [inputFormatter dateFromString:dateStr];
    return inputDate;
}


#pragma mark - 懒加载

- (NSMutableDictionary *)moth{
    if (!_moth) {
        _moth = [NSMutableDictionary dictionary];
        
    }
    return _moth;
}

- (NSMutableDictionary *)yearDic{
    if (!_yearDic) {
        _yearDic = [NSMutableDictionary dictionary];
        
    }
    return _yearDic;
}



@end
